package com.tp03g03.BD;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

public class BDAsadoManager {
	private Context context;
	private int dbVersion;
	BDAsadoHelper dbhelper;
	SQLiteDatabase db;

	public BDAsadoManager(Context context, int dbVersion) {
		this.context = context;
		this.dbVersion = dbVersion;
		dbhelper = new BDAsadoHelper(this.context, this.dbVersion);
		db = dbhelper.getWritableDatabase();
	}
	
	public void finalize(){
		cerrarDB();
	}
	
	public void cerrarDB(){
		if(db != null)
			db.close();
	}
	
	private int limpiarTabla(String tabla){
		int res = 0;

		db.beginTransaction();
		try {
			String strSQL = "DELETE FROM " + tabla;
			SQLiteStatement cmd = db.compileStatement(strSQL);
			cmd.execute();
			db.setTransactionSuccessful();
		} catch (Exception e) {
			db.endTransaction();
		}
		db.endTransaction();

		return res;	
	}
	
	
	public void borrarTablas(){
		limpiarTabla("CantidadInvitados");
		limpiarTabla("Comida");
		limpiarTabla("ContactosInvitados");
	}
	
	public int agregarInvitado(int genero, int cantidad){
		int res = 0;

		db.beginTransaction();
		try {
			String strSQL = "INSERT INTO CantidadInvitados (genero,cantidad) VALUES (" + genero + "," +cantidad + ")";
			SQLiteStatement cmd = db.compileStatement(strSQL);
			res = (int) cmd.executeInsert();
			db.setTransactionSuccessful();
		} catch (Exception e) {
			Log.e("DB", e.getMessage());
		}
		db.endTransaction();

		return res;		
	}
	
	public int agregarComida(int categoria, String nombre){
		int res = 0;

		db.beginTransaction();
		try {
			String strSQL = "INSERT INTO Comida (categoria,nombre) VALUES (" + categoria + ",?)";
			SQLiteStatement cmd = db.compileStatement(strSQL);
			cmd.bindString(1, nombre);
			res = (int) cmd.executeInsert();
			db.setTransactionSuccessful();
		} catch (Exception e) {
			Log.e("DB", e.getMessage());
		}
		db.endTransaction();

		return res;		
	}
	
	public int agregarContactoInvitado(int idContacto, String name, String email, boolean invitacionEnviada){
		int res = 0;

		db.beginTransaction();
		try {
			int iE = 0;
			if(invitacionEnviada) iE=1;
			String strSQL = "INSERT INTO ContactosInvitados (idContacto, name, email, invitacionEnviada) VALUES (" + idContacto + ",?,?," + iE + ")";
			SQLiteStatement cmd = db.compileStatement(strSQL);
			cmd.bindString(1, name);
			cmd.bindString(2, email);
			res = (int) cmd.executeInsert();
			db.setTransactionSuccessful();
		} catch (Exception e) {
			Log.e("DB", e.getMessage());
		}
		db.endTransaction();

		return res;		
	}
	
	public Cursor selectComida() {		
		return db.query(true, "Comida", new String[] { "categoria", "nombre" }, null,
				null, null, null, null, null);
	}
	
	public Cursor selectCantidadInvitados() {		
		return db.query(true, "CantidadInvitados", new String[] { "genero", "cantidad" }, null,
				null, null, null, null, null);
	}

	public Cursor selectInvitados(){
		return db.query(true, "ContactosInvitados", new String[] { "idContacto", "email", "name", "invitacionEnviada" }, null,
				null, null, null, null, null);
	}
	
}
